package com.qianfeng.service.impl;

import com.qianfeng.dao.UserDao;
import com.qianfeng.entity.User;
import com.qianfeng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户相关接口的实现类,包含用户的登陆、注册、注销
 *
 * @author mano233
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;


    @Override
    public User findUser(String name) throws Exception {
        return userDao.readByName(name);
    }

    @Override
    public User findUser(int uid) throws Exception {
        return userDao.readById(uid);
    }


    @Override
    public int registered(User user) throws Exception {
        User find = findUser(user.getUsername());
        if (find != null) {
            throw new Exception("该用户名已经注册");
        }
        userDao.create(user);
        return user.getId();
    }

    @Override
    public void updateInfo(User newUserInfo) throws Exception {
        if(newUserInfo.getId()==null){
            User user = findUser(newUserInfo.getUsername());
            if(user==null){
                throw new Exception("找不到用户："+newUserInfo.getUsername());
            }
            newUserInfo.setId(user.getId());
        }
        userDao.update(newUserInfo);
    }

}
